package com.drive.controller;

import com.drive.constant.MessageConstant;
import com.drive.constant.RedisMessageConstant;
import com.drive.entity.Result;
import com.drive.utils.SMSUtils;
import com.drive.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

/**
 * @author 往
 * 短信验证码
 */
@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {
    @Autowired
    private JedisPool jedisPool;

    //科目预约时发送短信验证码
    @RequestMapping("/send4Order")
    public Result send4Order(String telephone){
        //生成4位随机数字
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        try{
            //发送短信
            SMSUtils.sendShortMessage(telephone,code.toString());
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        //将生成的验证码缓存到redis中
        jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER,5*60,code.toString());
        return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }
}
